package com.example.itcacp.controller;

import com.example.itcacp.dto.response.Result;
import com.example.itcacp.entity.User;
import com.example.itcacp.repository.mybatis.UserMapper;
import com.example.itcacp.service.UserService;
import jakarta.annotation.Resource;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;


/**
* UserController用户控制器--用户相关业务逻辑处理
* */

@RestController  //RestController = Controller和ResponseBody注解的组合
@RequestMapping("/api/users")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    /**
     *  {@code @Autowired}
     *  根据对象的类型自动注入依赖对象、配置（required = false）来注入可能不存在的对象
     */
    @Resource //根据对象名称自动注入依赖对象
    private UserService userService;

    @Resource
    private UserMapper userMapper;
    /**
     * 登录验证
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {  //接收user
        User dbuser = userService.login ( user );
        return Result.success(dbuser); //返回给前端
    }

    /**
     * 注册验证
     */
    @RequestMapping("/register")
    public Result register(@RequestBody User user) {
        userService.register ( user );
        return Result.success ();
    }

    /**
     * 添加用户感兴趣标签
     * */
     @PostMapping("/addUserTags")
    public Result addUserTags(@RequestBody AddRequestBody addRequestBody) {
         User user = userMapper.findById ( addRequestBody.user_id );
         user.setTags ( addRequestBody.tags );
         userService.updateprofile ( user );
         return Result.success ( user );
     }
    // 封装前端请求传递过来的参数数据
     @Data
     static class AddRequestBody {
         private String user_id;
         private String tags;
    }

    @GetMapping("/getUser")
    public Result getUser(@RequestParam String userId) {
        User user = userService.getUser ( userId );
        return Result.success (user);
    }


}
